<template>
	<view class="device-list-container">
		<!-- 搜索框 -->
		<view class="search-box">
			<input 
				type="text"
				v-model="searchText"
				placeholder="请输入设备名称"
				placeholder-class="search-placeholder"
				@input="onSearch"
				class="search-input"
			/>
			<image 
				v-if="searchText"
				src="/static/images/clear.svg" 
				class="clear-icon" 
				mode="aspectFit"
				@tap="clearSearch"
			></image>
		</view>
		
		<!-- 设备列表 -->
		<scroll-view 
			class="device-list"
			scroll-y
			@scrolltolower="loadMore"
			@refresherrefresh="refresh"
			refresher-enabled
			:refresher-triggered="isRefreshing"
		>
			<view class="device-card" :class="{ 
				'online': device.active === true, 
				'offline': device.active === false 
			}" v-for="device in devices" :key="device.id.id" @tap="goToDetail(device)">
				<view class="card-header">
					<text class="device-name">{{ device.name }}</text>
					<text class="create-time">{{ formatTime(device.createdTime) }}</text>
				</view>
				
				<view class="device-info">
					<text class="device-type">{{ device.type }}</text>
					<view class="gateway-tag">{{ device.additionalInfo.gateway ? '网关' : '设备' }}</view>
				</view>
				
				<view class="divider"></view>
				
				<view class="status-row">
					<view class="device-status-tag" :class="{ 'online': device.active === true, 'offline': device.active === false }">
						{{ device.active === true ? '在线' : '离线' }}
					</view>
					<view class="more-btn">
						<view class="dot"></view>
						<view class="dot"></view>
						<view class="dot"></view>
					</view>
				</view>
			</view>
			
			<!-- 加载更多 -->
			<view class="loading" v-if="loading">
				<text>加载中...</text>
			</view>
			
			<!-- 无数据提示 -->
			<view class="no-data" v-if="devices.length === 0 && !loading">
				<image src="/static/images/no-data.svg" class="no-data-image" mode="aspectFit"></image>
				<text>暂无设备</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
import request from '@/utils/request.js'
import { formatTime } from '@/utils/timeUtils.js'

export default {
	data() {
		return {
			devices: [],
			page: 0,
			pageSize: 10,
			sortProperty: 'createdTime',
			sortOrder: 'DESC',
			loading: false,
			isRefreshing: false,
			hasMore: true,
			
			// 查询参数
			name: '',
			deviceProfileId: null,
			active: null,
			searchText: '',  // 添加搜索文本
			searchTimer: null  // 用于防抖
		}
	},
	
	onLoad(options) {
		console.log('options:', options)
		// 获取路由参数
		this.name = options.name || ''
		this.deviceProfileId = options.deviceProfileId || null
		this.active = options.active ? options.active : null
		
		// 设置标题
		uni.setNavigationBarTitle({
			title: this.name
		})
		
		// 加载设备列表
		this.fetchDevices()
	},
	
	methods: {
		// 移除本地的 formatTime 方法，改用导入的方法
		formatTime,
		
		// 获取设备列表
		async fetchDevices(isRefresh = false) {
			if (this.loading || (!this.hasMore && !isRefresh)) return
			
			this.loading = true
			
			try {
				// 构建查询参数
				const params = {
					pageSize: this.pageSize,
					page: isRefresh ? 0 : this.page,
					sortProperty: this.sortProperty,
					sortOrder: this.sortOrder,
					textSearch: this.searchText  // 添加搜索参数
				}
				
				// 添加可选参数
				if (this.deviceProfileId && this.deviceProfileId !== 'null') {
					params.deviceProfileId = this.deviceProfileId
				}
				// 只有当 active 不为 null 时才添加到查询参数
				if (this.active && this.active !== 'null') {
					params.active = this.active
				}
				
				const result = await request.get('/api/tenant/deviceInfos', params)
				
				if (result && result.data) {
					if (isRefresh) {
						this.devices = result.data
						this.page = 1
					} else {
						this.devices = [...this.devices, ...result.data]
						this.page++
					}
					
					this.hasMore = result.data.length === this.pageSize
				}
			} catch (error) {
				console.error('Failed to fetch devices:', error)
				uni.showToast({
					title: '获取设备列表失败',
					icon: 'none'
				})
			} finally {
				this.loading = false
				if (isRefresh) {
					this.isRefreshing = false
				}
			}
		},
		
		// 下拉刷新
		async refresh() {
			this.isRefreshing = true
			await this.fetchDevices(true)
		},
		
		// 加载更多
		loadMore() {
			if (!this.loading && this.hasMore) {
				this.fetchDevices()
			}
		},
		
		// 搜索处理
		onSearch(event) {
			// 清除之前的定时器
			if (this.searchTimer) {
				clearTimeout(this.searchTimer)
			}
			
			// 设置新的定时器，延迟300ms执行搜索
			this.searchTimer = setTimeout(() => {
				this.page = 0  // 重置页码
				this.fetchDevices(true)  // 重新加载数据
			}, 300)
		},
		
		// 清除搜索
		clearSearch() {
			this.searchText = ''
			this.page = 0
			this.fetchDevices(true)
		},
		
		// 添加跳转方法
		goToDetail(device) {
			uni.navigateTo({
				url: `/pages/device/deviceListDetail?deviceId=${device.id.id}`
			})
		}
	}
}
</script>

<style lang="scss">
.device-list-container {
	min-height: 100vh;
	background-color: #f5f5f5;
	padding: 20rpx;
}

.device-list {
	height: calc(100vh - 40rpx - 112rpx);  /* 减去搜索框的高度 */
}

.device-card {
	background: #FFFFFF;
	border-radius: 8px;
	padding: 12px 16px;
	margin: 10px 0;
	border-left: 5px solid transparent;
	box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	
	&.online {
		border-left-color: #4CAF50;
	}
	
	&.offline {
		border-left-color: #FF0000;
	}
	
	.card-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 6px;
		padding-right: 4px;
		
		.device-name {
			font-size: 18px;
			font-weight: 500;
		}
		
		.create-time {
			font-size: 15px;
			color: #999;
		}
	}
	
	.device-info {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 12px 0 6px;
		
		.device-type {
			font-size: 16px;
			color: #666;
		}
		
		.gateway-tag {
			font-size: 14px;
			color: #305680;
			background-color: rgba(48, 86, 128, 0.1);
			padding: 4px 12px;
			border-radius: 4px;
			margin-right: 4px;
		}
	}
	
	.divider {
		height: 1px;
		background-color: #EEEEEE;
		margin: 6px -16px;
		padding: 0 16px;
	}
	
	.status-row {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 8px;
		padding-right: 4px;
	}
	
	.device-status-tag {
		display: inline-block;
		padding: 4px 12px;
		border-radius: 4px;
		font-size: 14px;
		margin-top: 0;
		
		&.online {
			color: #4CAF50;
			background-color: rgba(76, 175, 80, 0.1);
		}
		
		&.offline {
			color: #FF0000;
			background-color: rgba(255, 0, 0, 0.1);
		}
	}
	
	.more-btn {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 21px;
		height: 21px;
		border: 1px solid #E0E0E0;
		border-radius: 50%;
		padding: 3px;
		background-color: #F3F5F8;
		
		.dot {
			width: 2.5px;
			height: 2.5px;
			border-radius: 50%;
			background-color: #305680;
			margin: 1.2px 0;
		}
	}
}

// 修改第一个卡片的上边距
.device-list .device-card:first-child {
	margin-top: 2px;
}

.loading,
.no-data {
	text-align: center;
	padding: 30rpx;
	color: #999;
	font-size: 28rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 20rpx;
}

.no-data-image {
	width: 200rpx;
	height: 200rpx;
}

.search-box {
	background-color: #fff;
	padding: 20rpx;
	margin-bottom: 20rpx;
	border-radius: 12rpx;
	display: flex;
	align-items: center;
	position: relative;
}

.search-input {
	flex: 1;
	height: 72rpx;
	font-size: 28rpx;
	color: #333;
	padding: 0 60rpx 0 20rpx;  /* 右侧留出清除图标的空间 */
	background-color: #f5f5f5;
	border-radius: 8rpx;
}

.search-placeholder {
	color: #999;
	font-size: 28rpx;
}

.clear-icon {
	position: absolute;
	right: 40rpx;
	width: 32rpx;
	height: 32rpx;
	padding: 10rpx;
}
</style> 